// pages/me/wantBuy/wantBuy.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        screenHeight: app.globalData.screenHeight,
        searchParams:{
            pageNum: 1,
            pageSize: 6,
            status: '',
            keyword:'',
            pages:'',
            total:'',
        },
        noData: true,
        wantBuyList:{},
        InitData:false,
        searchtype:null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    doNothing(){},
onLoad: function (options) {
        this.InitUserStatus();
        this.setData({
          'InitData': true
        })
    },
    onShow: function(){
      if (this.data.InitData) {
        this.goQuery();
      }
    },
    // 检查一下登录状态。
    async InitUserStatus(){
        // app.showLoading("获取用户信息");
        await app.checkToken();
        this.InitUserInfo();
        wx.hideLoading();
    },
    InitUserInfo(){
        let userInfo = app.globalData.userInfo;
        // 这里再验证一遍权限。
        if (userInfo.roleList.indexOf('manageWantBuy') == -1) {
            app.showNoAuthOrNoGrant('您无权操作查看此页面');
            return;
        }else{
            this.goQuery();
        }
    },
    clearQuery(){
      this.setData({
        'searchParams.keyword': '',
        'searchtype': 0
      });
      this.goQuery();
    },
    goQuery(e){
      let self = this;
      let searchtype = undefined ==  e?0:e.currentTarget.dataset.searchtype
      this.setData({
        'searchParams.pageNum':1,
        'searchParams.total':'',
        'searchParams.pages':'',
        'wantBuyList': {},
        'searchtype': searchtype
      });
      this.wantBuySelfList(this.data.searchParams);
    },
    wantBuySelfList(searchParams){
      searchParams.status == ""? delete searchParams.status:'';
      // 整理完成进行搜索操作。
      app.sHttp.wantBuySelfList(searchParams).then(res=>{
          let wantBuyList = this.data.wantBuyList;
          wantBuyList[searchParams.pageNum] = res.result.wantBuyList;
          if (0 == res.result.total) {
            this.setData({
              'noData': true
            })
          }else{
              this.setData({
                  'noData': false,
                  'wantBuyList': wantBuyList,
                  'searchParams.total':res.result.total,
                  'searchParams.pages': res.result.pages
                })
          }
        },err=>{
          app.error(err.msg);
        })
    },
    switchType(e){
        let type = e.currentTarget.dataset.type;
        let _this = this;
        this.setData({
            'searchParams.status': type
        },(res)=>{
            _this.clearQuery();
        })
    },
    bindtapInput(e){
      this.setData({
        'searchParams.keyword': e.detail.value
      })
    },
    onReachBottom(){
        if (this.data.searchParams.pageNum == this.data.searchParams.pages) {
            //app.alert('没有更多了哦~');
        }else{
            let self = this;
            this.setData({
                'searchParams.pageNum': self.data.searchParams.pageNum+1
            });
            this.wantBuySelfList(this.data.searchParams);
        }
    },
    previewImage(e){
      let num = e.currentTarget.dataset.num;
      let IndexItem = e.currentTarget.dataset.idx;
      let picIndex = e.currentTarget.dataset.picidx;
      let wantBuyItem = this.data.wantBuyList[num+''][IndexItem];
      let picList = [];
      wantBuyItem.picList.forEach(item =>{
        picList.push(item.picUrl);
      });
      wx.previewImage({
        current: picList[picIndex],
        urls: picList 
      })
    },
    // 编辑信息
    editWantBuy(e){
        let buyId = e.currentTarget.dataset.bid;
        wx.navigateTo({
          url: '/pages/me/wantBuy/info/info?bid=' + buyId
        })
    },
    // 重新上架
    reShelving(e){
      wx.showModal({
        title: '上架提醒',
        content: '确认上架该求购吗？',
        success(res) {
          if (res.confirm) {
            let buyId = e.currentTarget.dataset.bid;
            let operationPageNum = e.currentTarget.dataset.num;
            app.sHttp.wantBuySelfModifyStatus({buyId:buyId,status:1}).then(res =>{
              app.success('上架成功~');
              let self = this;
              setTimeout(() => {
                self.wantBuySelfList({pageNum:operationPageNum,pageSize:6,status:self.data.searchParams.status,keyword:self.data.searchParams.keyword});
              }, 1000);
            },err=>{
              app.error(err.msg);
            })
          }
        }})
    },
    // 下架
    offTheShelf(e){
      wx.showModal({
        title: '下架提醒',
        content: '确认下架该求购吗？',
        success(res) {
          if (res.confirm) {
            let buyId = e.currentTarget.dataset.bid;
            let operationPageNum = e.currentTarget.dataset.num;
            app.sHttp.wantBuySelfModifyStatus({buyId:buyId,status:2}).then(res =>{
                app.success('下架成功~');
                let self = this;
                setTimeout(() => {
                self.wantBuySelfList({pageNum:operationPageNum,pageSize:6,status:self.data.searchParams.status,keyword:self.data.searchParams.keyword});
                }, 1000);
            },err=>{
                app.error(err.msg);
            })
          }
        }
      })
    },
})